updateTable1();
updateTable2();

/**
 * 切换表格
 * 
 * @param {[type]} ) {
 *            $('#btn1').removeClass('active'); $(this).addClass('active'); var
 *            table1 [description]
 * @return {[type]} [description]
 */
$('#btn2').on('click', function() {

	$('#btn1').removeClass('active');
	$(this).addClass('active');
	var table1 = document.getElementById("table1");
	var table2 = document.getElementById("table2");
	table1.style.display = "none";
	table2.style.display = "";
	$('.checked').removeClass('on_check');
	$('.add-btn').removeClass('add-project-btn');
	$('.add-btn').addClass('add-industry-btn');
	$('.delect-btn').removeClass('delete-project-btn');
	$('.delect-btn').addClass('delete-industry-btn');

	// $('.check-input').attr('checked', false);

})

$('#btn1').on('click', function() {

	$('.checked').removeClass('on_check');

	$('#btn2').removeClass('active');
	$(this).addClass('active');

	var table1 = document.getElementById("table1");
	var table2 = document.getElementById("table2");
	table1.style.display = "";
	table2.style.display = "none";

	$('.add-btn').removeClass('add-industry-btn');
	$('.add-btn').addClass('add-project-btn');
	$('.delete-btn').removeClass('delete-industry-btn');
	$('.delete-btn').addClass('delete-project-btn');
	// $('.check-input').attr('checked', false);

})

/**
 * 添加项目类别
 * 
 * @param {[type]} ) {
 *            fadeModal('.add-project-modal');} [description]
 * @return {[type]} [description]
 */
$(document).on('click', '.add-project-btn', function() {

	fadeModal('.add-project-modal');

})

$('.btn-add-project').on('click', function() {
	if (validateProject() != undefined) {
		console.log("aa=" + validateProject().projectTypeName)
		console.log(validateProject())
		// PathVariable

		// RequestParam
		var projectTypeName = validateProject().projectTypeName;
		console.log("qdprojectTypeName=" + validateProject().projectTypeName);

		var options = getBASEPOSTAJAX();
		options.url = "/epi/admin/projectType/";
		options.data.projectTypeName = projectTypeName;
		options.success = function(data) {
			console.log("添加项目类别:" + data);
			// data = jQuery.parseJSON(data);
			var status = data.status;
			if (status == 500) {
				setAlert('非法操作！');
			} else if (status == 400) {
				setAlert('添加失败！');
			} else if (status == 200) {
				var as = $('.linkPage-input');
				console.log("as=" + as);
				// updateTable1($('.linkPage-input')[0].value);
				updateTable1($('.linkPage-input').value);
				setAlert('添加成功');
				$('.add-project-modal,.mask').fadeOut();

			} else {
				setAlert('系统繁忙，请刷新重试');
			}
			options.error = function() {
				setAlert('系统繁忙，请刷新重试');
			}

		}/* $.ajax(options) */;

	} else {
		console.log("validate");
		setAlert('系统繁忙，请刷新重试');
	}
	$.ajax(options)
});

/**
 * 添加行业类别
 * 
 * @param {[type]} ) {
 *            fadeModal('.add-industry-modal');} [description]
 * @return {[type]} [description]
 */
$(document).on('click', '.add-industry-btn', function() {

	fadeModal('.add-industry-modal');
})

$('.btn-add-industry').on('click', function() {

	if (validateIndustry() != undefined) {
		// PathVariable

		// RequestParam
		var industryTypeName = "testadd";
		console.log("qdindustryName=" + validateIndustry().industryTypeName);
		industryTypeName = validateIndustry().industryTypeName;
		var options = getBASEPOSTAJAX();
		options.url = "/epi/admin/industryType/";
		options.data.industryTypeName = industryTypeName;
		options.success = function(data) {
			console.log("添加行业类别:" + data)
			var status = data.status;
			if (status == 500) {
				setAlert('非法操作！');
			} else if (status == 400) {
				setAlert('添加失败！');
			} else if (status == 200) {
				// updateTable2($('.linkPage-input')[0].value);
				updateTable2($('.linkPage-input').value);
				setAlert('添加成功');
				$('.add-industry-modal,.mask').fadeOut();

			} else {
				setAlert('系统繁忙，请刷新重试');
			}
		}
		options.error = function() {

		}
		$.ajax(options);

	} else {
		setAlert('系统繁忙，请刷新重试');
	}

	$('.add-industry-modal,.mask').fadeOut();
})

/**
 * 修改项目类别信息
 * 
 * @param {[type]} ){
 *            fadeModal('.modify-project-modal');
 *            $(this).parents("tr").first().addClass('selected');
 *            $('.projectName').val($('.selected').children('td').eq(2).text());
 *            console.log($(this).children('input').val());
 *            $('.thisID-input').val($(this).children('input').val());}
 *            [description]
 * @return {[type]} [description]
 */
$(document).on('click', '.project-info-modify', function() {
	console.log($(this).children('.thisID-input').val());
	fadeModal('.modify-project-modal');
	$(this).parents("tr").first().addClass('selected');
	$('.projectName').val($('.selected').children('td').eq(2).text());

	$('.thisID-input').val($(this).children('input').val());
})

$('.btn-modify-project').on('click', function() {

	var thisId = $('.thisID-input').val();
	console.log($('.projectName').eq(1).val().length)
	if ($('.projectName').eq(1).val().length > 20) {
		setAlert('类别名称过长，请重新输入');
	} else if ($('.projectName').eq(1).val() == "") {
		setAlert('请输入类别名称');
	} else {
		// 测试数据
		// pathvariable
		var id = 1;
		console.log("qdid=" + thisId);
		id = thisId;

		// requestParam
		var projectTypeName = $('.projectName').eq(1).val();
		console.log("qdprojectTypeName=" + $('.projectName').eq(1).val())

		var options = getBASEPUTAJAX();
		options.url = "/epi/admin/projectType/" + id;
		options.data.projectTypeName = projectTypeName;
		options.success = function(data) {
			console.log("修改项目类别信息:" + data)
			var status = data.status;
			if (status == 500) {
				setAlert('非法操作！');
			} else if (status == 400) {
				setAlert('修改失败！');
			} else if (status == 200) {
				// updateTable1($('.linkPage-input')[0].value);
				updateTable1($('.linkPage-input').value);
				setAlert("修改成功！");
				$('.modify-project-modal,.mask').fadeOut();
			} else {
				setAlert('系统繁忙，请刷新重试');
			}
		}
		options.error = function() {
			setAlert('系统繁忙，请刷新重试');
		}
		// console.log(options)
		$.ajax(options);
	}

})

/**
 * 修改行业类别信息
 * 
 * @param {[type]} ){
 *            fadeModal('.modify-industry-modal');
 *            $(this).parents("tr").first().addClass('selected');
 *            $('.industryName').val($('.selected').children('td').eq(2).text());
 *            console.log($(this).children('input').val());
 *            $('.thisID-input').val($(this).children('input').val());}
 *            [description]
 * @return {[type]} [description]
 */
$(document).on('click', '.industry-info-modify', function() {

	fadeModal('.modify-industry-modal');
	$(this).parents("tr").first().addClass('selected');
	$('.industryName').val($('.selected').children('td').eq(2).text());
	console.log($(this).children('input').val());
	$('.thisID-input').val($(this).children('input').val());
})

$('.btn-modify-industry').on('click', function() {

	var thisId = $('.thisID-input').val();
	console.log($('.industryName').eq(1).val().length)
	if ($('.industryName').eq(1).val().length > 20) {
		setAlert('类别名称过长，请重新输入');
	} else if ($('.industryName').eq(1).val() == "") {
		setAlert('请输入类别名称');
	} else {
		// PathVariable
		var id = 1;
		console.log("qdid=" + $('.thisID-input').val());
		id = $('.thisID-input').val()

		// RequestParam
		var industryTypeName = "testupdate";
		console.log("qdindustryTypeName=" + $('.industryName').eq(1).val())
		industryTypeName = $('.industryName').eq(1).val();

		var options = getBASEPUTAJAX();
		options.url = "/epi/admin/industryType/" + id;
		options.data.industryTypeName = industryTypeName;
		options.success = function(data) {
			console.log("修改行业类别信息:" + data)
			var status = data.status;
			if (status == 500) {
				setAlert('非法操作！');
			} else if (status == 400) {
				setAlert('修改失败！');
			} else if (status == 200) {
				// updateTable2($('.linkPage-input')[0].value);
				updateTable2($('.linkPage-input').value);
				setAlert("修改成功！");
				$('.modify-industry-modal,.mask').fadeOut();
			} else {
				setAlert('系统繁忙，请刷新重试');
			}
		}
		options.error = function() {
			setAlert('系统繁忙，请刷新重试');
		}
		$.ajax(options
		/*
		 * { url : "/epi/admin/industryType/" + $('.thisID-input').val(), data : {
		 * _method : "PUT", industryTypeName : industryTypeName }, type :
		 * "POST", dataType : 'json', success : function(data) {
		 * console.log("修改行业类别信息:" + data) var status = data.status; if (status ==
		 * 500) { setAlert('非法操作！'); } else if (status == 400) {
		 * setAlert('修改失败！'); } else if (status == 200) {
		 * updateTable2($('.linkPage-input')[0].value); setAlert("修改成功！");
		 * $('.modify-industry-modal,.mask').fadeOut(); } else {
		 * setAlert('系统繁忙，请刷新重试'); } }, error : function() {
		 * setAlert('系统繁忙，请刷新重试'); } }
		 */)
	}

})

/**
 * 删除类别
 * 
 * @param {[type]} ) {
 *            fadeModal('.confirm-modal');} [description]
 * @return {[type]} [description]
 */
$(document).on('click', '.delete-project-btn', function() {
	fadeModal('.confirm-modal');
})

$(document).on('click', '.delete-industry-btn', function() {
	fadeModal('.confirm-modal');
})

$(document).on('click', '.confirm-btn', function() {
	if ($('button').hasClass('delete-project-btn')) {

		// 循环遍历勾选中的checkbox
		var j = 0;
		var thisId = new Array();
		var check = $('.checked.on_check');
		console.log(check.length)
		for (var i = 0; i < check.length; i++) {
			console.log(check.eq(i).children('.hideId').val())
			thisId[j] = check.eq(i).children('.hideId').val();
			console.log(thisId[j]);
			j++;
		}

		if (thisId.length == 0) {
			setAlert('请勾选要删除的项目类别');
		} else {
			// PathVariable

			// RequestParam
			var ids = [ 1, 2, 3 ]
			console.log("thisId=" + thisId);
			ids = thisId;

			var options = getBASEDELETEAJAX();
			options.url = "/epi/admin/projectType/";
			options.data.projectTypeIds = ids;
			options.success = function(data) {
				console.log(data)
				var status = data.status;
				if (status == 500) {
					setAlert('非法操作！');
				} else if (status == 400) {
					setAlert('删除失败！');
				} else if (status == 200) {
					$('tr').removeClass('selected');
					$(this).parents('tr').first().addClass('selected');
					$('.selected').remove();
					// updateTable1($('.linkPage-input')[0].value);
					updateTable1($('.linkPage-input').value);
					setAlert('删除成功');

				} else {
					setAlert('系统繁忙，请刷新重试');
				}
			};
			options.error = function() {
				setAlert('系统繁忙，请刷新重试');
			};
			$.ajax(options)
			$('.confirm-modal,.mask').fadeOut();

		}

	} else {

		var j = 0;
		var thisId = new Array();
		var check = $('.checked.on_check');
		console.log(check.length)
		for (var i = 0; i < check.length; i++) {
			console.log(check.eq(i).children('.hideId').val())
			thisId[j] = check.eq(i).children('.hideId').val();
			console.log(thisId[j]);
			j++;
		}

		if (thisId.length == 0) {
			setAlert('请勾选要删除的项目类别');
		} else {
			// PathVariable

			// RequestParam
			var ids = [ 1, 2, 3 ];
			console.log("qdids=" + thisId);
			ids = thisId;

			// 删除行业类别
			var options = getBASEDELETEAJAX();
			options.url = "/epi/admin/industryType/";
			options.data.industryTypeIds = ids;
			options.success = function(data) {
				console.log(data)
				var status = data.status;
				if (status == 500) {
					setAlert('非法操作！');
				} else if (status == 400) {
					setAlert('删除失败！');
				} else if (status == 200) {
					// updateTable2($('.linkPage-input')[0].value);
					updateTable2($('.linkPage-input').value);
					setAlert('删除成功');
				} else {
					setAlert('系统繁忙，请刷新重试');
				}
			}
			options.error = function() {
				setAlert('系统繁忙，请刷新重试');
			}

			$.ajax(options)
			$('.confirm-modal,.mask').fadeOut();
		}
	}
})

/**
 * 验证信息
 * 
 * @return {[type]} [description]
 */
function validateProject() {

	var projectName = $('.add-project-name').eq(0).val();

	if (projectName == "") {
		setAlert('请输入类别名称');
	} else if (projectName.length > 10) {
		setAlert('类别名称过长，请重新输入');
	} else {
		return {
			projectTypeName : projectName
		}
	}

}

function validateIndustry() {

	var industryName = $('.add-industry-name').eq(0).val();

	if (industryName == "") {
		setAlert('请输入类别名称');
	} else if (industryName.length > 10) {
		setAlert('类别名称过长，请重新输入');
	} else {
		return {
			industryTypeName : industryName

		}
	}

}

/**
 * 更新表格
 * 
 * @param {[type]}
 *            pageNum [description]
 * @param {[type]}
 *            records [description]
 * @return {[type]} [description]
 */
function updateTable1(pageNum, records) {

	if (records == undefined) {
		records = 20;
	}
	if (pageNum == undefined) {
		pageNum = 1;
	}

	// PathVariable
	var hdpageNum = 2;
	var hdrecords = 3;
	console.log("qdPageNum=" + pageNum)
	console.log("qdrecords=" + records)
	// RequestParam

	var options = getBASEGETAJAX();
	options.url = "/epi/admin/projectType/projectTypes/" + pageNum + "/"
			+ records;
	options.success = function(data) {
		console.log(JSON.stringify(data))
		var myTemplate = Handlebars.compile($("#view-template1").html());
		$('.project-line').html(myTemplate(data));
	};
	options.error = function() {
		setAlert('系统繁忙，请刷新重试');
	}

	$.ajax(options
	/*
	 * {
	 * 
	 * url : "/epi/admin/projectType/projectTypes/" + pageNum + "/" + records,
	 * type : "GET", dataType : 'json', success : function(data) {
	 * console.log(JSON.stringify(data)) var myTemplate =
	 * Handlebars.compile($("#view-template1").html());
	 * $('.project-line').html(myTemplate(data)); }, error : function() {
	 * setAlert('系统繁忙，请刷新重试'); } }
	 */);

}

// 行业类别
function updateTable2(pageNum, records) {

	if (records == undefined) {
		records = 20;
	}
	if (pageNum == undefined) {
		pageNum = 1;
	}

	$.ajax({
		// PathVariable

		// RequestParam

		url : "/epi/admin/industryType/industryTypes/" + pageNum + "/"
				+ records,
		type : "GET",
		dataType : 'json',
		success : function(data) {
			console.log(JSON.stringify(data))
			var myTemplate = Handlebars.compile($("#view-template2").html());
			$('.industry-line').html(myTemplate(data));
		},
		error : function() {
			setAlert('系统繁忙，请刷新重试');
		}
	});

}

/**
 * 翻页
 * 
 * @param {[type]} ) {
 *            $('.linkPage-input')[0].value++;
 *            showTable($('.linkPage-input')[0].value);} [description]
 * @return {[type]} [description]
 */
$('.upPage').click(function() {
	// $('.thisID-input').value = 1;
	// $('.linkPage-input')[0].value++;
	$('.linkPage-input').value++;

	// showTable($('.linkPage-input')[0].value);
	showTable($('.linkPage-input')[0].value);
})

$('.downPage').click(function() {
	// $('.thisID-input').value = 1;
	if ($('.linkPage-input').value > 1) {

		$('.linkPage-input').value--;
	} else {
		setAlert("已经是第一页了");
	}

	showTable($('.linkPage-input').value);

})

$(document).on('click', '.checked', function() {
	$(this).toggleClass('on_check');
})